﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using Zeus.Thunder.Web.ViewModels;
using Zeus.Thunder.Web.Download;
using log4net;

namespace Zeus.Thunder.Web.Controllers
{
    public class PurchasableFileGenerator : Controller
    {
        private static readonly ILog log = LogManager.GetLogger("loggerInfo");
        
        private IFileDownload fileDownload;

        public FileStreamResult Generate(PurchasableCriteria criteria, DownloadData data)
        {
            MemoryStream stream = null;
            string contentType = "";
            string fileName = "";

            if (criteria.DownType == DownType.EXCEL)
            {
                //Excel Download
                //stream = ServiceLocator.Current.GetInstance<IFileDownload>("ExcelDownload").Save(data);
                fileDownload = new ExcelDownload();
                contentType = "application/ms-excel";
                fileName = data.FileName + ".xls";
            }
            else
            {
                //Csv Download
                //stream = ServiceLocator.Current.GetInstance<IFileDownload>("CsvDownload").Save(data);
                fileDownload = new CsvDownload();
                contentType = "application/ms-txt";
                fileName = data.FileName + ".csv";
            }

            stream = fileDownload.Save(data);
            log.Info("File Generate - Head: " + data.TableHead + ", Body: " + data.TableBody + ", File Name: " + fileName);

            return File(stream, contentType, fileName);
        }
    }
}
